﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace CS544_Protocol_Client
{


    /**
    * Author: Kelly Page, Brian Driscoll
    * Date Created: 3/3/2011
    * Date Updated: 3/4/2011
    * 
    * Purpose: Handles all private messages to client.
    * 
    **/

    public class PrivateMessageManager
    {

        
        //message manager events
        public event EventHandler NewPrivateMessage;
        public event EventHandler MessageUpdate;
        public event EventHandler UserAway;
        public event EventHandler UserOnline;

        private List<PrivateMessage> PrivateMessages = new List<PrivateMessage>(); //holds all private messages
        private Dictionary<string,string> UpdatedMessages = new Dictionary<string,string>();//list of updated messages;
        public List<User> AwayList = new List<User>(); //list of away users
              

        //Conctructor
        public PrivateMessageManager()
        {
                                
        }
        

        //Create a new private message
        public void createMessage(string senderName)
        {

            PrivateMessage newMessage = new PrivateMessage(senderName);
            this.PrivateMessages.Add(newMessage);
            OnNewPrivateMessage();

        }

        
        //Add message to a current private message log
        public void addNewMessage(string sender, string message)
        {

            string theSender = sender;
            string newMessage = message;

            //find current private message to add the new message to
            foreach (PrivateMessage msg in this.PrivateMessages)
            {

                //if matches then add message
                if (theSender == msg.Username)
                {
                    msg.addNewMessage(sender, message);
                    this.UpdatedMessages.Add(sender,message);
                    OnMessageUpdate();
                }

            }

        }


        //Remove a private message instance from the list
        public void removeMessage(string ofSender)
        {
            string username = ofSender;
            int index = 0;
            //find message in list and remove
            foreach (PrivateMessage msg in this.PrivateMessages)
            {
                if (msg.Username == username)
                {
                    index = PrivateMessages.IndexOf(msg);
                }
            }

            PrivateMessages.RemoveAt(index);
           
        }


        //Get updated messages from all private messages and clear current list
        public Dictionary<string, string> getUpdatedMessages()
        {
            Dictionary<string, string> messages = this.UpdatedMessages;
            this.UpdatedMessages.Clear();

            return this.UpdatedMessages;            
        }


        //Searches for new already created private message
        public bool isNewSender(string sender)
        {

            string theSender = sender;
            bool isNewSender = true;

            foreach (PrivateMessage msg in this.PrivateMessages)
            {

                if (theSender == msg.Username)
                {
                    isNewSender = false;
                }
            
            }

            return isNewSender;
        
        }//end isNewSender()



        //Add user to away list
        public void addToAwayList(string username, string statusMessage)
        {
            User user = new User();
            user.setUsername(username);
            user.setStatus(statusMessage);

            this.AwayList.Add(user);

            OnUserAway();

        }


        //Remove user from away list
        public void removeFromAwayList(string username)
        {
            string toRemove = username;
            int index = 0;
            foreach (User user in this.AwayList)
            {

                if (user.Username == toRemove)
                {
                    index = AwayList.IndexOf(user);
                }
            }

            AwayList.RemoveAt(index);

        }
        


        private void OnMessageUpdate()
        {
            if (MessageUpdate != null)
                MessageUpdate(this, new EventArgs());
        }


        private void OnNewPrivateMessage()
        {
            if (NewPrivateMessage != null)
                NewPrivateMessage(this, new EventArgs());
        }


        private void OnUserAway()
        {
            if (UserAway != null)
                UserAway(this, new EventArgs());
        }
        

        private void OnUserOnline()
        {
            if (UserOnline != null)
                UserOnline(this, new EventArgs());
        }





    }
}
